//判断一个obj是否是数字
//obj is number
var isNumber = function(obj){
	return obj === +obj;
};

//判断一个obj是否是整数
//obj is integer
var isInteger = function(obj) {
	return (obj | 0) === obj;
};

//判断一个obj是否是布尔值
//obj is boolean
function isBoolean(obj) {
	return obj === !!obj
}

//深度拷贝
//Deep clone using JSON
var DeepClone = function(obj){
	return JSON.parse(JSON.stringify(obj));
};

//构造一个数组，并用某个值或某个构造函数对每一项进行初始化
//new Array and fill value
var CreateArray = function(startindex, length, value, valueIsConstructor){
	var arr = [];
	var endidx = startindex + length;
	if (valueIsConstructor){
		for (var idx = startindex; idx < endidx; idx++)
			arr[idx] = new value();
	}else{
		for (var idx = startindex; idx < endidx; idx++)
			arr[idx] = value;
	}
	return arr;
};

//构造一个数组，等于两个数组的公共部分
//create a new Array, containing items both in two given arrays
var GetIntersectArray = function(array1, array2){
	var array = [];
	for (var idx1 in array1){
		for (var idx2 in array2){
			if (array1[idx1] == array2[idx2])
				array[array.length] = array1[idx1];
		}
	}
	return array;
};

//两点之间的角度
//Heading degree of two point
//vector(1, 0) is 0, Clockwise is +, anticlockwise is -
var TwoPointHeading = function(basePoint, targetPoint){
	if (basePoint.x == targetPoint.x){
		return targetPoint.y > basePoint.y ? -90 : 90;
	}else{
		return -Math.atan2(targetPoint.y - basePoint.y, targetPoint.x - basePoint.x) / Math.PI * 180;
	}
};

//按字数分割字符串，汉字占2个字符
//split string to a string-array by size,
//chinese char as size 2, not-chinese char as size 1
var SplitText = function(text, size){
	var textArray = [];
	while (true){
		var splitted = false;
		var sizecount = 0;
		for (var idx = 0; idx < text.length; idx++){
			if (text.charCodeAt(idx) > 255){
				sizecount += 2;
			}else{
				sizecount += 1;
			}
			if (sizecount > size){
				textArray[textArray.length] = text.substring(0, idx);
				text = text.substring(idx);
				splitted = true;
				break;
			}
		}
		if (splitted == false){
			textArray[textArray.length] = text;
			break;
		}
	}
	return textArray;
};

//按长度格式化数字，使用K,M,B缩小整数，并控制小数位数
//format number of specific string-length, return string
var FormatNumber = function(number, length){
	var str = "" + number;
	if (str.length <= length)
		return str;
	var pointPos = str.indexOf(".");
	if (pointPos == -1 || pointPos > length){
		var clipCount = pointPos - length + 1;
		var clipNum = Math.pow(10, clipCount);
		var number2 = Math.round(number / clipNum) * clipNum;
		var divCount = str.length % 3;
		if (divCount > 3)
			divCount = 3;
		var number3 = number2 / Math.pow(1000, divCount);
		if (divCount == 0)
			return str;
		else if (divCount == 1)
			return "" + number3 + "K";
		else if (divCount == 2)
			return "" + number3 + "M";
		else if (divCount >= 3)
			return "" + number3 + "B";
	}else{
		var remainCount = length - pointPos - 1;
		var remainNum = Math.pow(10, remainCount);
		var number2 = Math.round(number * remainNum) / remainNum;
		var str2 = "" + number2;
		return str2.substr(0, length);
	}
};

//使一堆node对齐到水平线，并隔开固定间距
//Arrange Alignment, make nodes as a horizontal line, with specifice spacing
var ArrangeHorizontal = function(centerX, centerY, itemSpace, nodes){
	if (nodes.length <= 0)
		return;
	
	var wholeWidth = (nodes.length - 1) * itemSpace;
	for (var idx = 0; idx < nodes.length; idx++){
		wholeWidth += nodes[idx].width * nodes[idx].scaleX;
	}
	var posX = centerX - 0.5 * wholeWidth + 0.5 * nodes[0].width * nodes[0].scaleX;
	nodes[0].x = posX;
	nodes[0].y = centerY;
	for (var idx = 1; idx < nodes.length; idx++){
		posX += itemSpace + 0.5 * (nodes[idx - 1].width * nodes[idx - 1].scaleX + nodes[idx].width * nodes[idx].scaleX);
		nodes[idx].x = posX;
		nodes[idx].y = centerY;
	}
};

//使一堆node对齐到垂直线，并隔开固定间距
//Arrange Alignment, make nodes as a vertical line, with specifice spacing
var ArrangeVertical = function(centerX, centerY, itemSpace, nodes){
	if (nodes.length <= 0)
		return;

	var wholeHeight = (nodes.length - 1) * itemSpace;
	for (var idx = 0; idx < nodes.length; idx++){
		wholeHeight += nodes[idx].height * nodes[idx].scaleY;
	}
	var posY = centerY + 0.5 * wholeHeight - 0.5 * nodes[0].height * nodes[0].scaleY;
	nodes[0].x = centerX;
	nodes[0].y = posY;
	for (var idx = 1; idx < nodes.length; idx++){
		posY -= (itemSpace + 0.5 * (nodes[idx - 1].height * nodes[idx - 1].scaleY + nodes[idx].height * nodes[idx].scaleY));
		nodes[idx].x = centerX;
		nodes[idx].y = posY;
	}
};

//替换字符串
//Replace pattern of text with replaceString
var StringReplace = function(tgtStr, pattern, replaceStr, isMatchOne){
	var isfirst = isMatchOne == true;
	
	var str = tgtStr;
	var pos = str.indexOf(pattern);
	while (pos > -1){
		var tmp = str.substring(0, pos);
		var tmp2 = str.substring(pos + pattern.length);
		str = tmp + replaceStr + tmp2;
		pos = str.indexOf(pattern);
		if (isfirst)
			break;
	}
	return str;
};

//循环发送网络请求，停止循环
//Post a request by network
//example:	url = "http://127.0.0.1:8080/campaign/getuser"
//			data = "id=110"
//the full-URL will be : "http://127.0.0.1:8080/campaign/getuser?id=110"
//handler must be function like:  "function ReceiveHandler(string text, string setcookie){}"
//param "text" will be the return string, casually a jason-string
var _requestArray = [];
var _requestCursor = -1;
var _requestCookie = null;
var PostHttpRequest = function(url, data, handler, recalltime) {
	if (recalltime == null)
		recalltime = 1;
	
	var xhr = cc.loader.getXMLHttpRequest();
	
	xhr.open("POST", url);
	xhr.timeout = 5000;
	if (_requestCookie != null){
		xhr.setRequestHeader("Cookie",_requestCookie);
	}
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
			xhr.handled = true;
			var setcookie = _GetSessionid(xhr.getResponseHeader("Set-Cookie"));
			if (setcookie != null){
				_requestCookie = setcookie;
			}
			if (handler != null)
				handler(xhr.responseText);
		}
	};
	xhr.handled = false;
	xhr.send(data);
	
	//caching request
	_requestCursor = (_requestCursor+1) % 50;
	_requestArray[_requestCursor] = {xhr:xhr, url:url, data:data, handler:handler, recalltime:recalltime, recallhandle:null};
	_requestArray[_requestCursor].recallhandle = setTimeout(
			"_HandlePostTimeout(" + _requestCursor + ")", xhr.timeout + 10);
	return _requestCursor;
};
var CancelPostRecall = function(cursor){
	if (_requestArray[cursor].recallhandle != null){
		clearTimeout(_requestArray[cursor].recallhandle);
		_requestArray[cursor].recallhandle = null;
	}
};
var _HandlePostTimeout = function(cursor){
	var request = _requestArray[cursor];
	if (request.xhr.handled != true){
		request.xhr.abort();
		
		request.recalltime--;
		if (request.recalltime > 0){
			PostHttpRequest(request.url, request.data, request.handler, request.recalltime);
		}else{
			request.handler(null);
		}
	}
};
var _GetSessionid = function(text){
	if (text == null || text.length == null || text.length <= ("JSESSIONID").length)
		return null;

	var setcookie = text;
	var idx = setcookie.indexOf("JSESSIONID");
	if (idx < 0)
		return null;
	
	var idxend = setcookie.indexOf(";", idx);
	if (idxend <= idx){
		setcookie = setcookie.substring(idx);
	}else{
		setcookie = setcookie.substring(idx, idxend);
	}
	return setcookie;
};

//保存与服务器的时间差，生成与服务器同步的时间
//use RalibrationDatetime(realtime) to ralibration clock on client,
//then use "newDate()" instead of "new Date()" to get a real Datetime.
var _realDatetimeDiv = 0;
var RalibrationDatetime = function(realDatetime){
	_realDatetimeDiv = realDatetime - (new Date()).getTime();
};
var newDate = function(){
	var datetime = new Date();
	datetime.setTime(datetime.getTime() + _realDatetimeDiv);
	return datetime;
};